﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine;

namespace Miao.UI
{
    /// <summary>
    /// 拖拽物品并到可交换格子
    /// </summary>
    public class DragAndDropEvent : DragAndDrop
    {
        public object Data;
        public bool dragin { get; set; } = false;
        public int beforeIndex { get; set; } = -1;
        /// <summary>
        /// 绑定父类的序号
        /// </summary>
        public int index {
            get
            {
                if (!dragin)
                {
                    return this.transform.parent.GetSiblingIndex();
                }
                else
                {
                    return beforeIndex;
                }
            }
          }
        public event Action<DragAndDropEvent, PointerEventData> onBeginDrag;
        public event Action<DragAndDropEvent, PointerEventData> onDrag;
        public event Action<DragAndDropEvent,PointerEventData> onEndDrag;
        
        public override void OnBeginDrag(PointerEventData eventData)
        {
            beforeIndex = this.transform.parent.GetSiblingIndex();
            dragin = true;
            onBeginDrag?.Invoke(this, eventData);
            base.OnBeginDrag(eventData);
        }
        public override void OnDrag(PointerEventData eventData)
        {
            onDrag?.Invoke(this, eventData);
            base.OnDrag(eventData);
        }

        public override void OnEndDrag(PointerEventData eventData)
        {
            onEndDrag?.Invoke(this, eventData);
            base.OnEndDrag(eventData);
            beforeIndex = -1;
            dragin = false;
        }
    }
}
